<?php

namespace app\admin\model\ldcms;
use addons\ldcms\model\common\Backend;


class AllCategory extends Backend
{

    // 表名
    protected $name = 'ldcms_allcategory';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAdminTreeList()
    {
        $where = [];
        $data = $this->where('status', 2)
            ->where($where)
            ->field('id,pid,name,status')
            ->order($this->sort)->select();
        $data = $this->toSelectTree(collection($data)->toArray());
        return $data;
    }

    /** 数组转换成节点树 html
     * @param $list
     * @param int $pid
     * @param $level
     * @return array
     * @auther lxc
     */
    protected function toSelectTree($list, $pid = 0, $level = 0)
    {
        $list = $this->toMoreTree($list, 'pid', 'child_arr');
        /*匿名函数实现递归生成html_tree*/
        $function = function ($list, $level) use (&$function) {
            $arr = array();
            $number = 1;
            $total = count($list);
            foreach ($list as $k => $v) {
                if ($level == 0) {
                    $v['icon_name'] = $v['name'];
                }
                elseif ($number == $total) {
                    $v['icon_name'] = html_entity_decode(str_repeat('&nbsp;&nbsp;', $level) . '└ ' . $v['name']);
                }
                else {
                    $v['icon_name'] = html_entity_decode(str_repeat('&nbsp;&nbsp;', $level) . '├ ' . $v['name']);
                }
                $child_arr = isset($v['child_arr']) ? $v['child_arr'] : '';
                unset($v['child_arr']);
                $arr[] = $v;
                if (!empty($child_arr)) {
                    $arr = array_merge($arr, $function($child_arr, $level + 1));
                }
                $number++;
            }
            return $arr;
        };
        return $function($list, $level);
    }

    /** 列表转多维关联数组
     * @param $items  原始数据
     * @param string $pid  父ID
     * @param string $child_name 自定义子数组名称  默认child
     * @return array
     */
    protected function toMoreTree($items, $pid = "pid", $child_name = 'child')
    {
        $map = [];
        $tree = [];
        foreach ($items as &$it) {
            $map[$it['id']] = & $it;
        } //数据的ID名生成新的引用索引树
        foreach ($items as &$it) {
            @$parent = & $map[$it[$pid]];
            if ($parent) {
                $parent[$child_name][] = & $it;
            }
            else {
                $tree[] = & $it;
            }
        }
        return $tree;
    }


}
